package com.ruoyi.jkcrData.service.impl;

import com.alibaba.fastjson2.JSON;
import com.deepoove.poi.data.PictureRenderData;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.*;
import com.ruoyi.jkcrData.domain.pad.JkcrTaskReqDTO;
import com.ruoyi.jkcrData.domain.pad.JkcrTaskRespDTO;
import com.ruoyi.jkcrData.domain.pad.JkcrTreeDataItemDto;
import com.ruoyi.jkcrData.domain.plat.*;
import com.ruoyi.jkcrData.domain.report.JkcrReportData;
import com.ruoyi.jkcrData.service.*;
import com.ruoyi.task.domain.entity.DataTaskEntity;
import com.ruoyi.task.service.IDataTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;


/**
 * 架空乘人数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-28
 */
@Service
public class DataJkcrServiceImpl implements IDataJkcrService
{
    @Autowired
    private IDataTaskService dataTaskService;

    @Autowired
    private IDataJkcrGzyyService dataJkcrGzyyService;

    @Autowired
    private IDataJkcrHdqjService dataJkcrHdqjService;

    @Autowired
    private IDataJkcrJzhlService dataJkcrJzhlService;

    @Autowired
    private IDataJkcrQylService dataJkcrQylService;

    @Autowired
    private IDataJkcrYxsdService dataJkcrYxsdService;

    @Autowired
    private IDataJkcrZhchshxService dataJkcrZhchshxService;

    @Autowired
    private IDataJkcrZhchxxService dataJkcrZhchxxService;

    @Autowired
    private IDataJkcrZhdlService dataJkcrZhdlService;

    /**
     * 详情数据
     *
     * @param dataTaskId
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @Override
    public JkcrTaskReqDTO getDetailJkcr(Long dataTaskId) throws ExecutionException, InterruptedException {
        long startTime = System.currentTimeMillis();
        //测试数据基本信息
        Future<DataTaskEntity> taskFuture = dataTaskService.selectDataTask(dataTaskId);
        //工作压力
        Future<List<DataJkcrGzyy>> gzyyListFuture = dataJkcrGzyyService.selectDataJkcrGzyyByDataTaskId(dataTaskId);
        //巷道倾角
        Future<List<DataJkcrHdqj>> hdqjListFuture = dataJkcrHdqjService.selectDataJkcrHdqjByDataTaskId(dataTaskId);
        //紧张力
        Future<List<DataJkcrJzhl>> jzhlListFuture = dataJkcrJzhlService.selectDataJkcrJzhlByDataTaskId(dataTaskId);
        //牵引力
        Future<List<DataJkcrQyl>> qylListFuture = dataJkcrQylService.selectDataJkcrQylByDataTaskId(dataTaskId);
        //运行速度
        Future<List<DataJkcrYxsd>> yxsdListFuture = dataJkcrYxsdService.selectDataJkcrYxsdByDataTaskId(dataTaskId);
        //重车向上
        Future<List<DataJkcrZhchshx>> zhchshxListFuture = dataJkcrZhchshxService.selectDataJkcrZhchshxByDataTaskId(dataTaskId);
        //重车向下
        Future<List<DataJkcrZhchxx>> zhchxxListFuture = dataJkcrZhchxxService.selectDataJkcrZhchxxByDataTaskId(dataTaskId);
        //制动力
        Future<List<DataJkcrZhdl>> zhdlListFuture = dataJkcrZhdlService.selectDataJkcrZhdlByDataTaskId(dataTaskId);
        //添加到集合对象中
        List<Future<?>> futureList = new ArrayList<>();
        futureList.add(taskFuture);
        futureList.add(gzyyListFuture);
        futureList.add(hdqjListFuture);
        futureList.add(jzhlListFuture);
        futureList.add(qylListFuture);
        futureList.add(yxsdListFuture);
        futureList.add(zhchshxListFuture);
        futureList.add(zhchxxListFuture);
        futureList.add(zhdlListFuture);
        // 从future提取所有数据
        JkcrTaskRespDTO jkcrTaskRespDTO = new JkcrTaskRespDTO();
        for (Future<?> future : futureList) {
            while (true) {
                if (taskFuture.isDone() && !taskFuture.isCancelled()) {
                    // 获取结果
                    Object resultObject = future.get();
                    // 任务数据
                    if (resultObject instanceof DataTaskEntity) {
                        jkcrTaskRespDTO.setDataTaskEntity((DataTaskEntity) resultObject);
                    }
                    if (resultObject instanceof java.util.List) {
                        List<Object> resultList = (List<Object>) resultObject;
                        if (!resultList.isEmpty()) {
                            Object item = resultList.get(0);
                            //测试任务类型获取数据
                            if (item instanceof DataJkcrGzyy) {
                                jkcrTaskRespDTO.setGzyyEntityList(ListUtils.castList(resultList, DataJkcrGzyy.class));
                            }
                            if (item instanceof DataJkcrHdqj) {
                                jkcrTaskRespDTO.setHdqjEntityList(ListUtils.castList(resultList, DataJkcrHdqj.class));
                            }
                            if (item instanceof DataJkcrJzhl) {
                                jkcrTaskRespDTO.setJzhlEntityList(ListUtils.castList(resultList, DataJkcrJzhl.class));
                            }
                            if (item instanceof DataJkcrQyl) {
                                jkcrTaskRespDTO.setQylEntityList(ListUtils.castList(resultList, DataJkcrQyl.class));
                            }
                            if (item instanceof DataJkcrYxsd) {
                                jkcrTaskRespDTO.setYxsdEntityList(ListUtils.castList(resultList, DataJkcrYxsd.class));
                            }
                            if (item instanceof DataJkcrZhchshx) {
                                jkcrTaskRespDTO.setZhchshxEntityList(ListUtils.castList(resultList, DataJkcrZhchshx.class));
                            }
                            if (item instanceof DataJkcrZhchxx) {
                                jkcrTaskRespDTO.setZhchxxEntityList(ListUtils.castList(resultList, DataJkcrZhchxx.class));
                            }
                            if (item instanceof DataJkcrZhdl) {
                                jkcrTaskRespDTO.setZhdlEntityList(ListUtils.castList(resultList, DataJkcrZhdl.class));
                            }
                        }
                    }
                    break;
                } else {
                    // 轮询查询结果
                    Thread.sleep(10);
                }
            }
        }
        //目录结构拼接
        List<JkcrTreeDataItemDto> treeDataList = new ArrayList<>();
        // 运行速度测试
        if (null != jkcrTaskRespDTO.getYxsdEntityList() && !jkcrTaskRespDTO.getYxsdEntityList().isEmpty()) {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("yxsdEntityList");
            jkcrTaskRespDTO.getYxsdEntityList().forEach(item-> treeDataItemDto.getTestItemList().add(new JkcrTreeDataItemDto.ProjectTestItem(item.getId(), item.getSaveTime(), "")));
            treeDataList.add(treeDataItemDto);
        }else {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("yxsdEntityList");
            treeDataList.add(treeDataItemDto);
        }
        //重车向下测试
        if (null != jkcrTaskRespDTO.getZhchxxEntityList() && !jkcrTaskRespDTO.getZhchxxEntityList().isEmpty()) {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("zhchxxEntityList");
            jkcrTaskRespDTO.getZhchxxEntityList().forEach(item-> treeDataItemDto.getTestItemList().add(new JkcrTreeDataItemDto.ProjectTestItem(item.getId(), item.getSaveTime(), "")));
            treeDataList.add(treeDataItemDto);
        }else {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("zhchxxEntityList");
            treeDataList.add(treeDataItemDto);
        }
        //重车向上测试
        if (null != jkcrTaskRespDTO.getZhchshxEntityList() && !jkcrTaskRespDTO.getZhchshxEntityList().isEmpty()) {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("zhchshxEntityList");
            jkcrTaskRespDTO.getZhchshxEntityList().forEach(item-> treeDataItemDto.getTestItemList().add(new JkcrTreeDataItemDto.ProjectTestItem(item.getId(), item.getSaveTime(), "")));
            treeDataList.add(treeDataItemDto);
        }else {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("zhchshxEntityList");
            treeDataList.add(treeDataItemDto);
        }
        //工作压力测试
        if (null != jkcrTaskRespDTO.getGzyyEntityList() && !jkcrTaskRespDTO.getGzyyEntityList().isEmpty()) {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("gzyyEntityList");
            jkcrTaskRespDTO.getGzyyEntityList().forEach(item-> treeDataItemDto.getTestItemList().add(new JkcrTreeDataItemDto.ProjectTestItem(item.getId(), item.getSaveTime(), "")));
            treeDataList.add(treeDataItemDto);
        }else {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("gzyyEntityList");
            treeDataList.add(treeDataItemDto);
        }
        //倾角测试
        if (null != jkcrTaskRespDTO.getHdqjEntityList() && !jkcrTaskRespDTO.getHdqjEntityList().isEmpty()) {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("hdqjEntityList");
            jkcrTaskRespDTO.getHdqjEntityList().forEach(item-> treeDataItemDto.getTestItemList().add(new JkcrTreeDataItemDto.ProjectTestItem(item.getId(), item.getSaveTime(), "")));
            treeDataList.add(treeDataItemDto);
        }else {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("hdqjEntityList");
            treeDataList.add(treeDataItemDto);
        }
        // 牵引力测试
        if (null != jkcrTaskRespDTO.getQylEntityList() && !jkcrTaskRespDTO.getQylEntityList().isEmpty()) {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("qylEntityList");
            jkcrTaskRespDTO.getQylEntityList().forEach(item-> treeDataItemDto.getTestItemList().add(new JkcrTreeDataItemDto.ProjectTestItem(item.getId(), item.getSaveTime(), "")));
            treeDataList.add(treeDataItemDto);
        }else {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("qylEntityList");
            treeDataList.add(treeDataItemDto);
        }
        //制动力测试
        if (null != jkcrTaskRespDTO.getZhdlEntityList() && !jkcrTaskRespDTO.getZhdlEntityList().isEmpty()) {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("zhdlEntityList");
            jkcrTaskRespDTO.getZhdlEntityList().forEach(item-> treeDataItemDto.getTestItemList().add(new JkcrTreeDataItemDto.ProjectTestItem(item.getId(), item.getSaveTime(), "")));
            treeDataList.add(treeDataItemDto);
        }else {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("zhdlEntityList");
            treeDataList.add(treeDataItemDto);
        }
        //静张力测试
        if (null != jkcrTaskRespDTO.getJzhlEntityList() && !jkcrTaskRespDTO.getJzhlEntityList().isEmpty()) {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("jzhlEntityList");
            jkcrTaskRespDTO.getJzhlEntityList().forEach(item-> treeDataItemDto.getTestItemList().add(new JkcrTreeDataItemDto.ProjectTestItem(item.getId(), item.getSaveTime(), "")));
            treeDataList.add(treeDataItemDto);
        }else {
            JkcrTreeDataItemDto treeDataItemDto = new JkcrTreeDataItemDto("jzhlEntityList");
            treeDataList.add(treeDataItemDto);
        }
        jkcrTaskRespDTO.setJkcrTreeDataList(treeDataList);
        System.out.printf("任务ID为 %d 的“架空乘人测试任务”查询耗时 %d。", dataTaskId, (System.currentTimeMillis() - startTime));
        return jkcrTaskRespDTO;
    }

    /**
     * 修改提升机数据
     *
     * @param jkcrTaskReqDTO
     */
    @Override
    public int updateDetailJkcr(JkcrTaskReqDTO jkcrTaskReqDTO) {
        return 0;
    }

    /**
     * 删除架空乘人数据，变更数据状态为2
     *
     * @param dataTaskId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delAllJkcrData(Long dataTaskId) {
        //测试数据基本信息
        //变更任务状态
        try {
            Long[] dataTaskIds = new Long[] {dataTaskId};
            dataTaskService.deleteDataTaskByDataTaskIds(dataTaskIds, "00E");
        }catch (Exception e){
            e.printStackTrace();
        }
        //工作压力
        dataJkcrGzyyService.updateDataJkcrGzyyByDataTaskId(dataTaskId);
        //巷道倾角
        dataJkcrHdqjService.updateDataJkcrHdqjByDataTaskId(dataTaskId);
        //静张力
        dataJkcrJzhlService.updateDataJkcrJzhlByDataTaskId(dataTaskId);
        //牵引力
        dataJkcrQylService.updateDataJkcrQylByDataTaskId(dataTaskId);
        //运行速度
        dataJkcrYxsdService.updateDataJkcrYxsdByDataTaskId(dataTaskId);
        //重车向上
        dataJkcrZhchshxService.updateDataJkcrZhchshxByDataTaskId(dataTaskId);
        //重车向下
        dataJkcrZhchxxService.updateDataJkcrZhchxxByDataTaskId(dataTaskId);
        //制动力
        dataJkcrZhdlService.updateDataJkcrZhdlByDataTaskId(dataTaskId);
        return 1;
    }

    /**
     * 架空乘人数据导入报告
     */
    @Override
    public String dataJkcrImportReport(JkcrReportData data) {
        // 报告模板名称
        String template = data.getTempId();
        //测试任务信息
        DataTaskEntity dataTaskEntity = dataTaskService.selectDataTaskByDataTaskId(data.getDataTaskEntity().getDataTaskId());
        data.setDataTaskEntity(dataTaskEntity);
        //获取生成的工况图并转换png格式
        String rootPath = RuoYiConfig.getProfile() + RuoYiConfig.getChartPath() + "/";
        //导入报告中
        Map<String, Object> params = new HashMap<>();
        // 大曲线图的长宽
        int picW = 450;
        int picH = 250;
        // 小曲线图的长款
        int picSW = 280;
        int picSH = 156;
        DecimalFormat df2 = new DecimalFormat("####0.00");
        //基本检测信息
        if(null != data.getDataTaskEntity()) {
            params.put("受检单位", data.getDataTaskEntity().getShjdw());
            params.put("设备名称", data.getDataTaskEntity().getShbbh());
            params.put("主检姓名", data.getDataTaskEntity().getZhjxm());
            params.put("测试人员", data.getDataTaskEntity().getCshry());
            params.put("检测日期", DateUtils.dateTime(data.getDataTaskEntity().getTaskCreateTime()));
        }
        //架空乘人基本信息
        //运行速度参数
        if (null != data.getYxsdEntityList()) {
            //获取单条数据
            DataJkcrYxsd dataJkcrYxsd = data.getYxsdEntityList();
            params.put("最大速度", dataJkcrYxsd.getZdsd());
            params.put("启动加速度", dataJkcrYxsd.getQdjsd());
            params.put("运行时间", dataJkcrYxsd.getQdsj());
            if(StringUtils.isNotEmpty(data.getSdSvg())) {
                String sdImg = System.currentTimeMillis() + "-" + "速度曲线图.png";
                boolean res = ProcessPictureUtils.convertToPng(data.getSdSvg(), rootPath + data.getDataTaskEntity().getDataTaskId() + "/" + sdImg);
                String resString = res ? "成功" : "失败";
                System.out.println("速度曲线图导入:" + resString);
                params.put( "速度曲线", new PictureRenderData(picW, picH, rootPath + data.getDataTaskEntity().getDataTaskId() + "/" +sdImg));
            }
        }
        //重车下行参数
        if (null != data.getZhchxxEntityList()) {
            //获取单条数据
            DataJkcrZhchxx dataJkcrZhchxx = data.getZhchxxEntityList();
            if(StringUtils.isNotEmpty(dataJkcrZhchxx.getSdList())) {
                if(dataJkcrZhchxx.getSdList().startsWith("\"") && dataJkcrZhchxx.getSdList().endsWith("\"")) {
                    params.put("重车下行制动初速度", df2.format(JSON.parseArray(dataJkcrZhchxx.getSdList().substring(1, dataJkcrZhchxx.getSdList().length() - 1), Float.class).get(0)));
                }
            }
            params.put("重车下行制动时间", dataJkcrZhchxx.getZdshj());
            params.put("重车下行制动减速度", dataJkcrZhchxx.getZhdjsd());
            params.put("重车下行制动距离", dataJkcrZhchxx.getZhdjl());
            if(StringUtils.isNotEmpty(data.getZhchxxSvg())) {
                String zhchxxImg = System.currentTimeMillis() + "-" + "重车下行制动曲线图.png";
                boolean res = ProcessPictureUtils.convertToPng(data.getZhchxxSvg(), rootPath + data.getDataTaskEntity().getDataTaskId() + "/" + zhchxxImg);
                String resString = res ? "成功" : "失败";
                System.out.println("重车下行制动曲线图导入:" + resString);
                params.put( "重车下行制动曲线", new PictureRenderData(picW, picH, rootPath + data.getDataTaskEntity().getDataTaskId() + "/" +zhchxxImg));
            }
        }
        //重车上行参数
        if (null != data.getZhchshxEntityList()) {
            //获取单条数据
            DataJkcrZhchshx dataJkcrZhchshx = data.getZhchshxEntityList();
            if(StringUtils.isNotEmpty(dataJkcrZhchshx.getSdList())) {
                if(dataJkcrZhchshx.getSdList().startsWith("\"") && dataJkcrZhchshx.getSdList().endsWith("\"")) {
                    params.put("重车上行制动初速度", df2.format(JSON.parseArray(dataJkcrZhchshx.getSdList().substring(1, dataJkcrZhchshx.getSdList().length() - 1), Float.class).get(0)));
                }
            }
            params.put("重车上行制动时间", dataJkcrZhchshx.getZdshj());
            params.put("重车上行制动减速度", dataJkcrZhchshx.getZhdjsd());
            params.put("重车上行制动距离", dataJkcrZhchshx.getZhdjl());
            if(StringUtils.isNotEmpty(data.getZhchshxSvg())) {
                String zhchshxImg = System.currentTimeMillis() + "-" + "重车上行制动曲线图.png";
                boolean res = ProcessPictureUtils.convertToPng(data.getZhchshxSvg(), rootPath + data.getDataTaskEntity().getDataTaskId() + "/" + zhchshxImg);
                String resString = res ? "成功" : "失败";
                System.out.println("重车上行制动曲线图导入:" + resString);
                params.put( "重车上行制动曲线", new PictureRenderData(picW, picH, rootPath + data.getDataTaskEntity().getDataTaskId() + "/" +zhchshxImg));
            }
        }
        //油压参数
        if (null != data.getGzyyEntityList()) {
            //获取单条数据
            DataJkcrGzyy dataJkcrGzyy = data.getGzyyEntityList();
            params.put("最大油压", dataJkcrGzyy.getYyMax());
            if(StringUtils.isNotEmpty(data.getYySvg())) {
                String yyImg = System.currentTimeMillis() + "-" + "油压曲线图.png";
                boolean res = ProcessPictureUtils.convertToPng(data.getYySvg(), rootPath + data.getDataTaskEntity().getDataTaskId() + "/" + yyImg);
                String resString = res ? "成功" : "失败";
                System.out.println("油压曲线图导入:" + resString);
                params.put( "油压曲线", new PictureRenderData(picW, picH, rootPath + data.getDataTaskEntity().getDataTaskId() + "/" +yyImg));
            }
        }
        //巷道倾角参数
        if (null != data.getHdqjEntityList()) {
            //获取单条数据
            DataJkcrHdqj dataJkcrHdqj = data.getHdqjEntityList();
            params.put("巷道倾角", dataJkcrHdqj.getQjvel());
        }
        //牵引力参数
        if (null != data.getQylEntityList()) {
            //获取单条数据
            DataJkcrQyl dataJkcrQyl = data.getQylEntityList();
            params.put("牵引力", dataJkcrQyl.getQyl());
        }
        //制动力参数
        if (null != data.getZhdlEntityList()) {
            //获取单条数据
            DataJkcrZhdl dataJkcrZhdl = data.getZhdlEntityList();
            params.put("制动力", dataJkcrZhdl.getZhdl());
            params.put("额定牵引力", dataJkcrZhdl.getEdqyl());
            params.put("制动力比值", dataJkcrZhdl.getChazhi());
        }
        //静张力参数
        if (null != data.getJzhlEntityList()) {
            //获取单条数据
            DataJkcrJzhl dataJkcrJzhl = data.getJzhlEntityList();
            params.put("静张力", dataJkcrJzhl.getJzhl());
            params.put("钢丝绳破断力", dataJkcrJzhl.getPdl());
            params.put("静张力比值", dataJkcrJzhl.getChazhi() + "%");
        }
        String docRootPath = RuoYiConfig.getProfile() + RuoYiConfig.getDocumentPath() + "/";
        String docFileName = System.currentTimeMillis() + "-jkcr.docx";
        boolean isOk = ExportWordUtils.exportWord(RuoYiConfig.getProfile() + RuoYiConfig.getTemplatePath() + "/" + template, docRootPath, docFileName, params);
        System.out.println("isOK: 生成测试报告" + isOk + " docFileName: " + docFileName);
        return "/profile/document/" + docFileName;
    }
}
